// 登录相关的路由
// 导出一个不执行函数
import login from '@/views/login.ejs';
import axios from '@/api/index.js';
export default function (router) {
    // 第三步 ：构建前端路由 
    router.route("/login", (req, res) => {
      
        res.render(login({ title: "hello" }))
        // 获取登录的用户名 校验用户名是否正确；
        let usernameEle = document.querySelector(".username"); 
        let pwdEle = document.querySelector(".pwd")
        document.querySelector(".exchange").innerHTML = "";
        // 把用户名发送到后端校验是否正确 
        usernameEle.onblur = async function(){
            let username = usernameEle.value;
            // console.log(username);
          let {data} = await  axios.post("/checkusername",{username});
          console.log(data);
          document.querySelector(".exchange").innerHTML = data.info;
        }

        // "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2NzkzMDI1MTAsImV4cCI6MTY3OTMwOTcxMH0.saCiBtuNGuFT-VZ-BkpUZPt13MAPqpfZCB3wKvjD2QU"
        // 校验用户名且密码 是否正确 
        let loginBtn = document.querySelector(".loginStyle");
        loginBtn.onclick = async function(){
            let username = usernameEle.value;
            let pwd = pwdEle.value;
            // console.log(username,pwd);
          let {data,tokenstr} = await  axios.post("/checkuser",{username,pwd});
          console.log(data);
          if(data.status===1){
                // 用户名且密码正确
                console.log(data);
                let {username,tokenstr} = data;
                localStorage.setItem("username",username);
                localStorage.setItem("token",tokenstr);
                // router.go("/admin/users");
                window.location.href = "/admin/users";
          }else{
            alert(data.info);
          }
        }
    })
}